package com.lz.service;

import com.lz.dao.CommonDao;
import com.lz.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by lz on 2018/5/19.
 */
@Service
@Transactional
public class CommonService {

    @Autowired
    private CommonDao commonDao;

    /**
     * Fund 查询所有基金
     */
    public List<Map<String, Object>> queryFund(String keyword, String pageIndex, String pageSize, String sortType, String field){
        switch (field){
            case "1":
                field = "start";
                break;
            case "2":
                field = "new_value";
                break;
            case "3":
                field = "riskLevel";
                break;
            default:
                field = "riskLevel";
        }
        return commonDao.queryFund(keyword,pageIndex,pageSize,sortType,field);
    }

    /**
     * Current 查询所有活期
     * @return
     * @param keyword
     * @param pageIndex
     * @param pageSize
     * @param sortType
     * @param field
     */
    public List<Map<String, Object>> queryCurrent(String keyword, String pageIndex, String pageSize, String sortType, String field){
        switch (field){
            case "1":
                field = "start_my";
                break;
            case "2":
                field = "get_my";
                break;
            case "3":
                field = "seven_sy";
                break;
            default:
                field = "start_my";
        }
        return commonDao.queryCurrent(keyword,pageIndex,pageSize,sortType,field);
    }

    /**
     * netloan 查询所有网贷
     * @return
     * @param keyword
     * @param pageIndex
     * @param pageSize
     * @param sortType
     * @param field
     */
    public List<Map<String, Object>> queryNetloan(String keyword, String pageIndex, String pageSize, String sortType, String field){

        return commonDao.queryNetloan(keyword,pageIndex,pageSize);
    }

    /**
     * NetloanAuxiliary  查询网贷备用
     * @return
     */
    public List<Map<String, Object>> queryNetloanAuxiliary(){
        return commonDao.queryNetloanAuxiliary();
    }

    /**
     * 查询所有定期
     * @param keyword
     * @param initInvestAmount
     * @param investPeriod
     * @param riskLevel @return
     * @param pageIndex
     * @param pageSize
     * @param sortType
     * @param field     */
    public List<Map<String, Object>> queryRegularByRule(String keyword, String initInvestAmount, String investPeriod, String riskLevel, String pageIndex, String pageSize, String sortType, String field){
        switch (field){
            case "1":
                field = "principal";
                break;
            case "2":
                field = "investPeriod";
                break;
            case "3":
                field = "riskLevel";
                break;
            default:
                field = "riskLevel";
        }
        return commonDao.queryRegularByRule(keyword,initInvestAmount,investPeriod,riskLevel,pageIndex,pageSize,sortType,field);
    }

    /**
     * 关键词查询定期
     * @param keyword
     * @return
     */
    public List<Map<String, Object>> queryRegular(String keyword){
        return commonDao.queryRegular(keyword);
    }

    /**
     * 查询所有
     * @param keyword
     * @param i
     * @return
     */
    public List<Map<String, Object>> queryAll(String keyword, int i){
        return commonDao.queryAll(keyword,i);
    }

    /**
     * 查询分页总数
     * @param product
     * @return
     */
    public Map<String, Object> queryNum(String product){
        String table = "";
        switch (product){
            case "1"://活期
                table = "current";
                break;
            case "2"://定期
                table = "regular";
                break;
            case "3"://基金
                table = "fund";
                break;
            case "4"://网贷
                table = "netloan";
                break;
        }
        return commonDao.queryNum(table);

    }


    /**
     * 通过关键字查询
     * @param keyword 关键字
     * @param product 产品类型
     * @param initInvestAmount 投入金额
     * @param investPeriod  投资期
     * @param riskLevel  风险水平
     * @param pageSize 每页显式多少条记录
     * @param pageIndex 起始页
     * @param sortType  正倒序
     * @param field  排序字段
     */
    public PageUtil queryByKeyword(String keyword, String product, String initInvestAmount, String investPeriod, String riskLevel, String pageSize, String pageIndex, String sortType, String field) throws Exception{
        PageUtil pageUtil = new PageUtil();
        if (StringUtils.isBlank(pageIndex)){
            pageIndex = "1";
        }
        if (StringUtils.isBlank(pageSize)){
            pageSize = "20";
        }

        final Map<String, Object> amountMap = this.queryNum(product);
        pageUtil.setPageSize(Integer.parseInt(pageSize));
        pageUtil.setPageIndex(Integer.parseInt(pageIndex));
        Long amount = (Long)amountMap.get("amount");
        pageUtil.setAllRowsAmount(amount.intValue());

        switch (sortType){
            case "1":
                sortType = "asc";
                break;
            case "2":
                sortType = "desc";
                break;
            default:
                sortType = "desc";
        }
        List<Map<String, Object>> list;
        switch (product){
            case "1"://活期
                list = this.queryCurrent(keyword,pageIndex,pageSize,sortType,field);
                break;
            case "2"://定期
                list = this.queryRegularByRule(keyword,initInvestAmount,investPeriod,riskLevel,pageIndex,pageSize,sortType,field);
                break;
            case "3"://基金
                list = this.queryFund(keyword,pageIndex,pageSize,sortType,field);
                break;
            case "4"://网贷
                list = this.queryNetloan(keyword,pageIndex,pageSize,sortType,field);
                break;
            default:
                list = new LinkedList<>();
        }
        pageUtil.setItems(list);
        pageUtil.calculatePage();
        return pageUtil;
    }

    /**
     * 查询全部
     * @param keyword
     * @return
     */
    public List queryByKeyword1(String keyword){
        List list = new ArrayList();
        for (int i = 0;i <= 3 ;i++){
            List<Map<String, Object>> maps = this.queryAll(keyword, i);
            if (maps.size() > 0){
                list.add(maps);
            }
        }
        return list;
    }

    /**
     * 通过id查询产品信息
     * @param product
     * @param pId
     */
    public List<Map<String, Object>> queryByProductId(String product, String pId) throws Exception{
        String table = "";
        switch (product){
            case "1"://活期
                table = "current";
                break;
            case "2"://定期
                table = "regular";
                break;
            case "3"://基金
                table = "fund";
                break;
            case "4"://网贷
                table = "netloan";
                break;
        }
        return commonDao.queryByProductId(table,pId);
    }
}
